package concurrent.customExecutor;

import utils.DateTimeUtils;

import java.util.Date;

/**
 * Author: Jason Wu
 * Date  : 2014-03-07
 */
public class ExecuteThread implements Runnable {
    private DynamicThreadPoolExecutor dynamicThreadPool;

    public ExecuteThread(DynamicThreadPoolExecutor dynamicThreadPool) {
        this.dynamicThreadPool = dynamicThreadPool;
    }

    @Override
    public void run() {
        for (int i = 0; i < 500; i++) {
            dynamicThreadPool.execute(new PrintTask(i));
        }
    }

    public static class PrintTask implements Runnable {
        private int index;

        public PrintTask(int index) {
            this.index = index;
        }

        @Override
        public void run() {
            try {
                System.out.println(String.format("TaskId:%d, Time:%s", index, DateTimeUtils.formatDate(new Date(), DateTimeUtils.DATE_TIME_FORMAT)));
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        public int getIndex() {
            return index;
        }
    }
}
